package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myapplication.homepage.ShouYeFragment;
import com.example.myapplication.message.MessageFragment;
import com.example.myapplication.mine.WoDeTaoBaoFragment;
import com.example.myapplication.shoppingcar.GouWuCheFragment;
import com.example.myapplication.video.ShiPinFragment;


public class MainActivity extends AppCompatActivity {
    LinearLayout llshouye, llshipin, llxiaoxi, llwodetaobao, llgouwuche;
    ShouYeFragment shouYeFragment = new ShouYeFragment();
    ShiPinFragment shiPinFragment = new ShiPinFragment();
    MessageFragment messageFragment = new MessageFragment();
    GouWuCheFragment gouWuCheFragment = new GouWuCheFragment();
    WoDeTaoBaoFragment woDeTaoBaoFragment = new WoDeTaoBaoFragment();
    ImageView ivshouye, ivshipin, ivxiaoxi, ivgouwuche, ivwodetaobao;
    TextView tvshouye, tvshipin, tvxiaoxi, tvgouwuche, tvwodetaobao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setupButtonListeners();
    }

    private void initEvent() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.theBegin, shouYeFragment).commit();
        ivshouye.setImageResource(R.drawable.zhuye2);
        tvshouye.setTextColor(getResources().getColor(R.color.blue));

    }

    private void setupButtonListeners() {
        llshouye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDaoHang();
                ivshouye.setImageResource(R.drawable.zhuye2);
                tvshouye.setTextColor(getResources().getColor(R.color.blue));
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.theBegin, shouYeFragment).commit();

            }
        });

        llshipin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDaoHang();
                ivshipin.setImageResource(R.drawable.shipin2);
                tvshipin.setTextColor(getResources().getColor(R.color.blue));
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.theBegin, shiPinFragment).commit();
            }
        });
        llxiaoxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDaoHang();
                ivxiaoxi.setImageResource(R.drawable.xiaoxi2);
                tvxiaoxi.setTextColor(getResources().getColor(R.color.blue));
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.theBegin, messageFragment).commit();
            }
        });
        llgouwuche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDaoHang();
                ivgouwuche.setImageResource(R.drawable.gouwuche2);
                tvgouwuche.setTextColor(getResources().getColor(R.color.blue));
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.theBegin, gouWuCheFragment).commit();
            }
        });

        llwodetaobao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDaoHang();
                ivwodetaobao.setImageResource(R.drawable.wodetaobao2);
                tvwodetaobao.setTextColor(getResources().getColor(R.color.blue));
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.theBegin, woDeTaoBaoFragment).commit();
            }
        });


    }

    private void initView() {
        llshouye = findViewById(R.id.ll_shouye);
        llshipin = findViewById(R.id.ll_shipin);
        llxiaoxi = findViewById(R.id.ll_xiaoxi);
        llgouwuche = findViewById(R.id.ll_gouwuche);
        llwodetaobao = findViewById(R.id.ll_wodetaobao);

        ivshouye = findViewById(R.id.iv_shouye);
        ivshipin = findViewById(R.id.iv_shipin);
        ivxiaoxi = findViewById(R.id.iv_xiaoxi);
        ivgouwuche = findViewById(R.id.iv_gouwuche);
        ivwodetaobao = findViewById(R.id.iv_wodetaobao);

        tvshouye = findViewById(R.id.tv_shouye);
        tvshipin = findViewById(R.id.tv_shipin);
        tvxiaoxi = findViewById(R.id.tv_xiaoxi);
        tvgouwuche = findViewById(R.id.tv_gouwuche);
        tvwodetaobao = findViewById(R.id.tv_wodetaobao);
    }

    private void initDaoHang() {
        ivshouye.setImageResource(R.drawable.zhuye);
        tvshouye.setTextColor(getResources().getColor(R.color.black));
        ivshipin.setImageResource(R.drawable.shipin);
        tvshipin.setTextColor(getResources().getColor(R.color.black));
        ivgouwuche.setImageResource(R.drawable.gouwuche);
        tvgouwuche.setTextColor(getResources().getColor(R.color.black));
        ivxiaoxi.setImageResource(R.drawable.xiaoxi);
        tvxiaoxi.setTextColor(getResources().getColor(R.color.black));
        ivwodetaobao.setImageResource(R.drawable.wodetaobao);
        tvwodetaobao.setTextColor(getResources().getColor(R.color.black));
    }

}